package com.delivery.www.dao.DaoImpl;

import com.delivery.www.dao.BaseDao;
import com.delivery.www.dao.CartDao;
import com.delivery.www.po.Cart;
import com.delivery.www.util.JdbcUtils;

import java.sql.Connection;
import java.util.List;

/**
 * @author 12970
 */
public class CartDaoImpl implements CartDao {
    @Override
    public int addCart(int userId, int storeId) {
        Connection connection = JdbcUtils.getConnection();
        Object[] params = {userId, storeId};
        String sql = "insert into cart (user_id,store_id) values (?,?)";
        int i = 0;
        if (connection != null) {
            i = BaseDao.executeUpdate(connection, sql, params);
            JdbcUtils.closeResource(connection, null, null);
        }
        return i;
    }

    @Override
    public Cart getCart(int userId, int storeId) {
        Connection connection = JdbcUtils.getConnection();
        Object[] params = {userId, storeId};
        String sql = "select cart_id,user_id,store_id from cart where user_id=? and store_id=?";
        Cart cart = null;
        if (connection != null) {
            cart = BaseDao.getForOne(connection, Cart.class, sql, params);
            JdbcUtils.closeResource(connection, null, null);
        }
        return cart;
    }

    @Override
    public List<Cart> getAllCart(int userId) {
        Connection connection = JdbcUtils.getConnection();
        Object[] params = {userId};
        String sql = "select cart_id,user_id,store_id from cart where user_id = ?";
        List<Cart> carts = null;
        if (connection != null) {
            carts = BaseDao.getList(connection, Cart.class, sql, params);
            JdbcUtils.closeResource(connection, null, null);
        }
        return carts;
    }
}
